package com.mtask.web.service.impl;

import com.mtask.common.dto.TaskJobDTO;
import com.mtask.common.dto.TaskJobResponseDTO;
import com.mtask.common.entity.task.TaskGroup;
import com.mtask.common.entity.task.TaskJob;
import com.mtask.task.repository.JobRepository;
import com.mtask.web.repository.TaskGroupRepository;
import com.mtask.web.service.TaskCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaskCrudServiceImpl implements TaskCrudService {

  @Autowired private TaskGroupRepository taskGroupRepository;
  @Autowired private JobRepository jobRepository;

  @Override
  public void save(TaskJobDTO dto) {
    TaskJob taskJob;
    if (dto.getIsUpdate()) {
      taskJob = convertForUpdate(dto);
    } else {
      taskJob = convertDTO2Entity(dto);
    }
    jobRepository.save(taskJob);
  }

  @Override
  public void deleteById(Integer id) {
    jobRepository.deleteById(id);
  }

  @Override
  public Page<TaskJobResponseDTO> findAll(PageRequest pageable) {
    Page<TaskJob> page = jobRepository.findAll(pageable);
    List<TaskJobResponseDTO> content =
        page.getContent().stream()
            .map(
                taskJob -> {
                  TaskJobResponseDTO responseDTO = new TaskJobResponseDTO();
                  BeanCopier beanCopier =
                      BeanCopier.create(TaskJob.class, TaskJobResponseDTO.class, false);
                  beanCopier.copy(taskJob, responseDTO, null);

                  responseDTO.setProjectName(taskJob.getTaskGroup().getTaskProject().getName());
                  responseDTO.setGroupName(taskJob.getTaskGroup().getGroupName());
                  responseDTO.setTaskGroupId(taskJob.getTaskGroup().getId());
                  responseDTO.setTaskProjectId(taskJob.getTaskGroup().getTaskProject().getId());
                  return responseDTO;
                })
            .collect(Collectors.toList());
    return new PageImpl<>(content, page.getPageable(), page.getTotalElements());
  }

  public TaskJob convertDTO2Entity(TaskJobDTO taskJobDTO) {
    TaskGroup taskGroup =
        taskGroupRepository
            .findById(taskJobDTO.getTaskGroupId())
            .orElseThrow(() -> new RuntimeException("参数错误"));
    final BeanCopier beanCopier = BeanCopier.create(TaskJobDTO.class, TaskJob.class, false);
    TaskJob taskJob = new TaskJob();
    beanCopier.copy(taskJobDTO, taskJob, null);
    taskJob.setTaskGroup(taskGroup);
    return taskJob;
  }

  public TaskJob convertForUpdate(TaskJobDTO taskJobDTO) {
    TaskJob taskJob =
        jobRepository
            .findById(taskJobDTO.getTaskJobId())
            .orElseThrow(() -> new RuntimeException("参数错误"));

    final BeanCopier beanCopier = BeanCopier.create(TaskJobDTO.class, TaskJob.class, false);
    beanCopier.copy(taskJobDTO, taskJob, null);
    return taskJob;
  }
}
